$(function () {
    //注册一个全局变量pageNumber，用作储存分页页码
    window.pageNumber = 1;
    //给顶部导航条注册点击事件
    $(".navbar-mmvtc .navbar-nav").on("click", "li.swiper-slide", function (event) {
        var urlParams = getUrlParams();
        var path = urlParams.path;
        $(".navbar-mmvtc .navbar-collapse").toggleClass("collapse in collapse");
        // window.history.pushState(null, null, $(this).data('id'));
        var path = $(this).data("cateno"); //获取data-cateno的值
        window.location.hash = path; //设置锚点
        loadInner(path);
    });
    $.ajax({
        statusCode: {
            404: function() {
                alert("page not found");
            }
        }
    });
    function loadingEffect() {
        var loading = $('#fountainG');
        $("#content").fadeTo(0, 0);
        loading.hide();
        $(document).ajaxStart(function () {
            $("#content").fadeTo(0, 0);
            loading.show();
        }).ajaxStop(function () {
            loading.hide();
            $("#content").fadeTo(400, 1);
        });
    }
    loadingEffect();

    loadInner(getUrlParams().path);
    var type = '';
    $(".navbar-mmvtc .navbar-nav li.swiper-slide ul.dropdown").on('click','li',function (e) {
        type = $(this).data('type');
        if (type == 'url'){
            e.stopPropagation();
        }
    });
    //监听地址栏变化
    $(window).off('hashchange').on('hashchange', function () {
        var urlParams = getUrlParams();
        var path = urlParams.path;
        var query = urlParams.query; //获取url中"?"符后的字串
        //获取栏目名
        if (path) {
            var cateNo = path.match(/^[a-zA-Z0-9\u4e00-\u9fa5]*/)[0];
            var navLi = $(".navbar-mmvtc .navbar-nav li.swiper-slide").filter("li[data-cateno=" + cateNo + "]");
            if (cateNo == navLi.data('cateno')) {
                navLi.addClass("current").siblings().removeClass("current"); //当前单前所处栏目
            }
        }
        type = type == '' ? query.type : type;
        if ($(".navbar-mmvtc .navbar-nav li.swiper-slide.current").data('cateno') == path && type != 'url') {
            type = '';
            return;
        }
        if (JSON.stringify(query) != "{}" || !$.isEmptyObject(query)) {
            query.type = type;
            loadInner(path, query);
            type = '';
            return;
        }

        loadInner(path);

    }).trigger('hashchange');
    //如果滚动的距离大于导航栏距离页面顶部的距离，则固定导航栏
    var navTop = $('.navbar-mmvtc').offset().top;
    $(document).on('scroll',function () {
        var scrollTop = $(document).scrollTop();
        navTop <= scrollTop ? $('.navbar-mmvtc').addClass('nav-fixed') : $('.navbar-mmvtc').removeClass('nav-fixed');
    })
});